﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NStore.Domain;
using NStore.Infrastructure.Repositories;
using NStore.Infrastructure.Security;
using Rhino.Mocks;

namespace NStore.Infrastructure.Tests.Security
{
    [TestClass]
    public class NStoreMembershipProviderTest
    {
        [TestMethod]
        public void ValidateUser_WithUserExist_ShouldBeCallOnValidatePasswordAtUser()
        {
            User mockUser = MockRepository.GenerateMock<User>();
            UserRepository repository = MockRepository.GenerateStub<UserRepository>();
            repository.Stub(r => r.GetByUserName("username")).Return(mockUser);
            NStoreMembershipProvider provider = new NStoreMembershipProvider(repository);
            
            provider.ValidateUser("username", "password");

            mockUser.AssertWasCalled(p => p.ValidatePassword("password"));
        }

        [TestMethod]
        public void ValidateUser_WithUserNonExist_ShouldBeFalse()
        {
            UserRepository repository = MockRepository.GenerateStub<UserRepository>();
            repository.Stub(r => r.GetByUserName("username")).Return(null);
            NStoreMembershipProvider provider = new NStoreMembershipProvider(repository);

            Assert.IsFalse(provider.ValidateUser("username", "password"));
        }

        [TestMethod]
        public void GetUser_ShouldBeCallOnGetByUserNameAtUserRepository()
        {
            
            UserRepository mock = MockRepository.GenerateMock<UserRepository>();
            NStoreMembershipProvider provider = new NStoreMembershipProvider(mock);

            provider.GetUser("username", true);

            mock.AssertWasCalled(p => p.GetByUserName("username"));
        }
    }
}
